<?php 
class Front_Payment_Generic extends System_Db_Generic  {
	public function getpaymentlist($params){
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_Preauth_Get_translist NULL,'".$params['preauth_datefrom']."','".$params['preauth_dateto']."','".$params['userid']."','".$params['menu_num']."'";
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();
		foreach ($results as $key=>$value){
			$results[$key]['preauth_datetime'] = getDateFormat2($results[$key]['preauth_datetime']);	
		}
		return $results;
	}

	public function getPaymentId(){
		$db = Zend_Registry :: get("db");
		//$statement = $db->prepare('select dbo.FN_Payment_GenID () as payment_id');
		$cmd_exec = "exec dbo.sp_elink_common_GenID 'payment'";
		$statement = $db->prepare($cmd_exec);
		
		$statement->execute();
		$results = $statement->fetchAll();
		//return $results['0']['payment_id'];
		return $results['0']['new_id'];
	}
	public function getPreAuthById($preauth_id,$user_id){
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_Preauth_Get_translist '".$preauth_id."',NULL,NULL,'".$user_id."',NULL";
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();
		foreach ($results as $key=>$value){
			$results[$key]['preauth_datetime'] = getDateFormat2($results[$key]['preauth_datetime']);
		}
		return $results;
	}
	public function getPolicyById($policy_id){
		if(!$policy_id) return;
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_member_queryByPolicyID '$policy_id'";
		//echo $cmd_exec; exit();
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();
		foreach ($results as $key=>$value){
			$results[$key]['effective'] = getDateFormat2($results[$key]['effective']);
			$results[$key]['expiry'] = getDateFormat2($results[$key]['expiry']);
			$results[$key]['me_cover'] = number_format($results[$key]['me_cover'],2,'.',',');
			$results[$key]['mc_cover'] = number_format($results[$key]['mc_cover'],2,'.',',');
			//$results[$key]['deductible'] = number_format($results[$key]['deductible'],2,'.',',');
			$results[$key]['copayment'] = number_format($results[$key]['copayment'],2,'.',',');
		}
		//_print($results);exit(); //--beerz
		return $results['0'];
	}
	
	public function getPlan2catByPlanId($plan_id,$policy_id){ 
		if(!$plan_id) return;
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_plan2cat_byplanid '$plan_id','$policy_id'";
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();
		return $results['0'];
	}
	
	public function insertPayment($params,$payment_id,$browser,$client_ip){
		$db = Zend_Registry :: get("db");
		if($params['last_acc']	!= '0'){ $params['acc_date']	=	$params['last_acc']; }
		$cmd_exec = "exec dbo.sp_elink_Payment_Trans_Save '".$payment_id."','".$params['userid']."','".$params['preauth_id']."','".$params['policy_no']."','".$params['payment_datetime']."',
				'".$params['payment_status']."','".$params['customer_type']."','".$params['policy_id']."','".$params['payment_type']."','".$params['acc_date']."',
				'".$params['admis_date']."','".$params['charge']."','".$params['discount']."','".$params['deductible']."','".$params['co_payment']."',
				'".$params['pre_approved']."','".$params['non_covered']."','".$params['indiation_admit']."','".$params['menu_seq']."','".$browser."','".$client_ip."'";
		$statement = $db->prepare($cmd_exec);
		$results = $statement->execute();
		
		return $results;
	}
	
	public function getClaimCover(){
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_Claim_Get_Cover_Type NULL";
		//echo $cmd_exec; exit();
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();
		return $results;
	}
	public function getAccList($policy_id,$menu_type){
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_Claim_Get_Acc_List '$policy_id','$menu_type'";
		//echo $cmd_exec;exit(); 
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();
		
		foreach ($results as $key=>$value){
			//$results[$key]['effective'] = getDateFormat2($results[$key]['effective']);
			//$test_acc	=	'2011-12-15 17:00:00.000';
			$results[$key]['cover_remain'] = number_format($results[$key]['cover_remain'],2,'.',','); //--beer
			if($results[$key]['acc_date'] != ''){
			$day	=	explode(" ",$results[$key]['acc_date']);
			$dmy	= 	explode("-",$day[0]);
			$d	=	$dmy[2];
			$m	=	$dmy[1];
			$y	=	$dmy[0];
			 
			$time 		= 	explode(".",$day[1]);
			$time1	=	$time[0];
			$time2	=	$time[1];
			$t				=	explode(":",$time[0]);
			$hour		=	$t[0];
			$min		=	$t[1];
			$lastacc	=	$hour.":".$min;
			
			$results[$key]['acc_date']	= $d."/".$m."/".$y." ".$lastacc	;
			} else {
				$results[$key]['acc_date']	= '0'	;
			}
		}
		return $results;
	}
	public function getClaimPreApp($params){
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_Claim_PreApv_Cal '".$params['preauth_id']."','".$params['menu_seq']."','".$params['cover_type']."','".$params['acc_date']."','".$params['admis_date']."','".$params['charge']."','".$params['discount']."','".$params['Deductible']."','".$params['lastAdmisOPD']."','".$params['er_followin']."'";		
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$_arr = $statement->fetchAll();
		$result = "[";
    	if($_arr){
    		$deduct = str_replace("\"","",number_format($_arr['0']['deduct'],2,'.',','));
    		//$deduct = str_replace("\"","",$_arr['0']['deduct']); 	//--beerz for test
    		$copay = str_replace("\"","",number_format($_arr['0']['copay'],2,'.',','));
    		//$copay = str_replace("\"","",$_arr['0']['copay']);	//--beerz for test
    		$preapv = str_replace("\"","",number_format($_arr['0']['preapv'],2,'.',','));
    		$noncover = str_replace("\"","",number_format($_arr['0']['noncover'],2,'.',','));
    		$result .= "{\"deduct\":\"$deduct\"," .
    				   " \"copay\":\"$copay\"," .
    				   " \"preapv\":\"$preapv\"," .
    				   " \"noncover\":\"$noncover\"" .
    				   " }";
    	}
    	$result .= "]";   	    	 
    	return $result;
	}
	//--beerz
	public function getDeducList($policy_id){
		$db = Zend_Registry :: get("db");
		$statement = $db->prepare("SELECT * FROM [dbo].[tf_getDeductibleUsed] (   null  ,null  ,null  ,null  ,null  ,'$policy_id'  ,null)");
		$statement->execute();
		$results = $statement->fetchAll();
		foreach ($results as $key=>$value){
			$results[$key]['dgroup'] = getDateFormat2($results[$key]['dgroup']);
			//$results[$key]['deduc'] = number_format($results[$key]['deduc'],2,'.',',');
		}
		return $results;
	}
	public function getLastAdmisOPD($policy_id){
		$db = Zend_Registry :: get("db");
		$statement = $db->prepare("select dbo.FN_Claim_LastAdmis_OPDClaim ('".$policy_id."') as last_admis");
		$statement->execute();
		$results = $statement->fetchAll();
		return $results['0']['last_admis'];
	}
	
}
?>